package com.cskaoyan.service;

import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketStorage;
import com.cskaoyan.bean.po.MarketStorageExample;
import com.cskaoyan.mapper.MarketStorageMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Auther: Yang Yi-zhou
 * @Date: 2022/9/7 13:52
 * @Description:
 */
@Service
public class StorageServiceImpl implements StorageService {

    @Autowired
    MarketStorageMapper marketStorageMapper;

    @Override
    public CommonData<MarketStorage> listStorage(Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page, limit);
        MarketStorageExample example = new MarketStorageExample();
        example.setOrderByClause(sort + " " + order);
        List<MarketStorage> marketStorages = marketStorageMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(marketStorages);
        return CommonData.data(pageInfo);
    }

    @Override
    public boolean deleteStorage(MarketStorage marketStorage) {
        int rows = marketStorageMapper.deleteByPrimaryKey(marketStorage.getId());
        return rows > 0 ? true : false;
    }

    @Override
    public MarketStorage updateStorage(MarketStorage marketStorage) {
        marketStorage.setUpdateTime(new Date());
        int rows = marketStorageMapper.updateByPrimaryKeySelective(marketStorage);
        MarketStorage marketStorage1 = marketStorageMapper.selectByPrimaryKey(marketStorage.getId());
        return marketStorage1;
    }
}
